﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using negocios;
using objetos;

namespace softsales.contabilidade
{
    public partial class frmCFOP : softsales.template.frmBase
    {
        public frmCFOP()
        {
            InitializeComponent();
        }

        BLL_TB_CFOP b_tb_cfop = new BLL_TB_CFOP();
        OBJ_TB_CFOP o_tb_cfop = new OBJ_TB_CFOP();

        private void frmCFOP_Load(object sender, EventArgs e)
        {
            grdCFOP.Focus();
            grdCFOP.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            CarregaGrid();

            tipoComboBox.Items.Add("E");
            tipoComboBox.Items.Add("S");
        }


        public override void btnExcluir_Click(object sender, EventArgs e)
        {
            base.btnExcluir_Click(sender, e);
        }

        public override void btnEditar_Click(object sender, EventArgs e)
        {

            string id = string.Empty;

            foreach (DataGridViewRow item in grdCFOP.Rows)
            {
                if (item.Selected)
                {
                    id = item.Cells["id"].Value.ToString();
                }

            }

            foreach (OBJ_TB_CFOP o_tb_cfop in b_tb_cfop.SelecionarLista(string.Format("tb_cfop.id = {0}", id), ""))
            {
                idTextBox.Text = o_tb_cfop.id.ToString();
                descricaoTextBox.Text = o_tb_cfop.descricao.ToString();
                tipoComboBox.SelectedIndex = tipoComboBox.FindStringExact(o_tb_cfop.tipo);
                cfopTextBox.Text = o_tb_cfop.cfop.ToString();

            }

            base.btnEditar_Click(sender, e);
            tabContainer.SelectedTab = tabFormulario;


        }

        public override void btnNovo_Click(object sender, EventArgs e)
        {
            base.btnNovo_Click(sender, e);
            tabContainer.SelectedTab = tabFormulario;
            idTextBox.Enabled = false;
            LimpaControles(this);


        }

        public override void btnCancelar_Click(object sender, EventArgs e)
        {

            base.btnCancelar_Click(sender, e);

            tabContainer.SelectedTab = tabLista;
        }

        public override void LimpaControles(Control ctl)
        {
            base.LimpaControles(ctl);
        }

        public override void btnLocalizar_Click(object sender, EventArgs e)
        {
            frmCFOPLocalizar fCFOPLocalizar = new frmCFOPLocalizar();

            if (fCFOPLocalizar.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                List<string> where = new List<string>();

                if (fCFOPLocalizar.id != string.Empty)
                    where.Add(string.Format(" id = {0}", fCFOPLocalizar.id));

                if (fCFOPLocalizar.descricao != string.Empty)
                    where.Add(string.Format(" descricao LIKE  '%{0}%'", fCFOPLocalizar.descricao));

                if (fCFOPLocalizar.cfop != string.Empty)
                    where.Add(string.Format(" cfop LIKE  '%{0}%'", fCFOPLocalizar.cfop));

                List<OBJ_TB_CFOP> lista_cfop = b_tb_cfop.SelecionarLista(string.Join("and", where.ToArray()).ToString(), "");


                if (lista_cfop.Count > 0)
                {
                    grdCFOP.DataSource = lista_cfop;
                    grdCFOP.Refresh();
                }
                else
                {
                    MessageBox.Show("Nenhum registro encontrado.");
                }



            }
            //base.btnLocalizar_Click(sender, e);

        }

        public override void btnSalvar_Click(object sender, EventArgs e)
        {
            base.btnSalvar_Click(sender, e);
        }

        public override bool Editar()
        {
            return base.Editar();
        }

        public override bool Localizar()
        {
            return base.Localizar();
        }

        public override bool Salvar()
        {
            try
            {
                o_tb_cfop.descricao = descricaoTextBox.Text;
                o_tb_cfop.cfop = Convert.ToInt32(cfopTextBox.Text);
                o_tb_cfop.tipo = tipoComboBox.GetItemText(tipoComboBox.SelectedItem);                

                if (idTextBox.Text != string.Empty)
                {
                    o_tb_cfop.id = Convert.ToInt32(idTextBox.Text);
                    b_tb_cfop.Alterar(o_tb_cfop);
                }
                else
                {
                    b_tb_cfop.Incluir(o_tb_cfop);
                }
                tabContainer.SelectedTab = tabLista;
                CarregaGrid();
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }

        public override bool Excluir()
        {
            try
            {
                o_tb_cfop.id = Convert.ToInt32(idTextBox.Text);
                b_tb_cfop.Excluir(o_tb_cfop);
                tabContainer.SelectedTab = tabLista;
                CarregaGrid();
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }

        public override void btnFechar_Click(object sender, EventArgs e)
        {
            base.btnFechar_Click(sender, e);
        }

        public override void CarregaGrid()
        {
            grdCFOP.DataSource = b_tb_cfop.SelecionarLista();
            grdCFOP.Refresh();
        }

        

     
    }
}
